package com.urbanski.util;

public class NumberUtils
{
	
	/**
	 * Parses the specified String to an int using Integer.parseInt, but returns the specified
	 * default if parsing fails (a NumberFormatException occurs).
	 * 
	 * @param s the String to parse
	 * @param d the default to return if parsing fails
	 * @return the String parsed as an int, or the default if parsing fails
	 */
	public static int parseInt(String s, int d)
	{
		if (s != null && s.length() > 0)
		{
			try
			{
				return Integer.parseInt(s);
			}
			catch (NumberFormatException e)
			{
				return d;
			}
		}
		
		return d;
	}
	
	/**
	 * Parses the specified String to a double using Double.parseDouble, but returns the specified
	 * default if parsing fails (a NumberFormatException occurs).
	 * 
	 * @param s the String to parse
	 * @param d the default to return if parsing fails
	 * @return the String parsed as a double, or the default if parsing fails
	 */
	public static double parseDouble(String s, double d)
	{
		if (s != null && s.length() > 0)
		{
			try
			{
				return Double.parseDouble(s);
			}
			catch (NumberFormatException e)
			{
				return d;
			}
		}
		
		return d;
	}
	
	/**
	 * Parses the specified String to a long using Long.parseLong, but returns the specified
	 * default if parsing fails (a NumberFormatException occurs).
	 * 
	 * @param s the String to parse
	 * @param d the default to return if parsing fails
	 * @return the String parsed as a long, or the default if parsing fails
	 */
	public static long parseLong(String s, long d)
	{
		if (s != null && s.length() > 0)
		{
			try
			{
				return Long.parseLong(s);
			}
			catch (NumberFormatException e)
			{
				return d;
			}
		}
		
		return d;
	}
	
	/**
	 * Parses the specified String to a short using Short.parseShort, but returns the specified
	 * default if parsing fails (a NumberFormatException occurs).
	 * 
	 * @param s the String to parse
	 * @param d the default to return if parsing fails
	 * @return the String parsed as a short, or the default if parsing fails
	 */
	public static short parseShort(String s, short d)
	{
		if (s != null && s.length() > 0)
		{
			try
			{
				return Short.parseShort(s);
			}
			catch (NumberFormatException e)
			{
				return d;
			}
		}
		
		return d;
	}
	
	/**
	 * Parses the specified String to a byte using Byte.parseByte, but returns the specified
	 * default if parsing fails (a NumberFormatException occurs).
	 * 
	 * @param s the String to parse
	 * @param d the default to return if parsing fails
	 * @return the String parsed as a byte, or the default if parsing fails
	 */
	public static byte parseByte(String s, byte d)
	{
		if (s != null && s.length() > 0)
		{
			try
			{
				return Byte.parseByte(s);
			}
			catch (NumberFormatException e)
			{
				return d;
			}
		}
		
		return d;
	}
	
	/**
	 * Parses the specified String to a float using Float.parseFloat, but returns the specified
	 * default if parsing fails (a NumberFormatException occurs).
	 * 
	 * @param s the String to parse
	 * @param d the default to return if parsing fails
	 * @return the String parsed as a float, or the default if parsing fails
	 */
	public static float parseFloat(String s, float d)
	{
		if (s != null && s.length() > 0)
		{
			try
			{
				return Float.parseFloat(s);
			}
			catch (NumberFormatException e)
			{
				return d;
			}
		}
		
		return d;
	}
	
	/**
	 * Attempts to parse the specified String, but returns null instead of throwing a NumberFormatException
	 * @param s the String to parse
	 * @return the parsed String, or null if a NumberFormatException would occur
	 */
	public static Integer tryParseInt(String s)
	{
		try
		{
			return Integer.parseInt(s);
		}
		catch (NumberFormatException e)
		{
			return null;
		}
	}
	
	/**
	 * Attempts to parse the specified String, but returns null instead of throwing a NumberFormatException
	 * @param s the String to parse
	 * @return the parsed String, or null if a NumberFormatException would occur
	 */
	public static Long tryParseLong(String s)
	{
		try
		{
			return Long.parseLong(s);
		}
		catch (NumberFormatException e)
		{
			return null;
		}
	}
	
	/**
	 * Attempts to parse the specified String, but returns null instead of throwing a NumberFormatException
	 * @param s the String to parse
	 * @return the parsed String, or null if a NumberFormatException would occur
	 */
	public static Double tryParseDouble(String s)
	{
		try
		{
			return Double.parseDouble(s);
		}
		catch (NumberFormatException e)
		{
			return null;
		}
	}
	
	/**
	 * Attempts to parse the specified String, but returns null instead of throwing a NumberFormatException
	 * @param s the String to parse
	 * @return the parsed String, or null if a NumberFormatException would occur
	 */
	public static Byte tryParseByte(String s)
	{
		try
		{
			return Byte.parseByte(s);
		}
		catch (NumberFormatException e)
		{
			return null;
		}
	}
	
	/**
	 * Attempts to parse the specified String, but returns null instead of throwing a NumberFormatException
	 * @param s the String to parse
	 * @return the parsed String, or null if a NumberFormatException would occur
	 */
	public static Short tryParseShort(String s)
	{
		try
		{
			return Short.parseShort(s);
		}
		catch (NumberFormatException e)
		{
			return null;
		}
	}
	
	/**
	 * Attempts to parse the specified String, but returns null instead of throwing a NumberFormatException
	 * @param s the String to parse
	 * @return the parsed String, or null if a NumberFormatException would occur
	 */
	public static Float tryParseFloat(String s)
	{
		try
		{
			return Float.parseFloat(s);
		}
		catch (NumberFormatException e)
		{
			return null;
		}
	}
}
